package com.study.web;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;


/**
 * @author 作者 grq
 * @version 创建时间：2018年8月4日 下午1:33:10
 * @Title: BaseController.java
 * @Package cn.zhuzi.sys.control
 * @Description: TODO(基本controller)
 */
@Slf4j
@Controller
public class SysBaseController {
    @Resource
    protected HttpServletRequest req;
    @Resource
    protected HttpServletResponse resp;

    @Resource
    protected ServletContext servletContext;


    /**
     * 使用此方法，普通方法可以得到request对象
     */
    protected HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    @RequestMapping("/")
    @ResponseBody
    public Date index() {
        return new Date();
    }

    @PostConstruct
    public void setBasePath() {
        try {
            log.info("系统路径和名称初始化工作");
            String contextPath = servletContext.getContextPath();
            servletContext.setAttribute("proPath", contextPath);
            servletContext.setAttribute("nams", "thymeleaf学习");
            log.info("contextPath is :{}", contextPath);
            //List<Menu> query = jdbcTemplate.query("select * from t_menu", new BeanPropertyRowMapper(Menu.class));
            //System.out.println("AdminController.getALL()");
            //for (Menu menu : query) {
            //	System.out.println(menu);
            //}
        } catch (Exception e) {
            log.info("系统路径和名称初始化失败");
        }
    }
}
